package com.sinacom.ibuilding.service;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;


import com.ibatis.dao.client.DaoManager;
import com.sinacom.ibuilding.beans.IdCreate;
import com.sinacom.ibuilding.dao.DaoConfig;
import com.sinacom.ibuilding.dao.iface.IdCreateDao;

public class IdCreateService {
	private static Logger logger = Logger.getLogger(IdCreateService.class);
	private IdCreateDao idCreateDao;
	TransActionService ts = new TransActionService();
	public IdCreateService(){
		DaoManager	daoManager = DaoConfig.getDaoManager();
		idCreateDao = (IdCreateDao) daoManager.getDao(IdCreateDao.class);
	}
	
	public Integer generateId(String table){
	    
	    try {
            ts.startTransaction();
            Map map = new HashMap();
            map.put("name", table);
            IdCreate idCreate = idCreateDao.get(map);
            Integer curValue = idCreate.getValue(); 
            idCreate.setValue(new Integer(curValue.intValue() + 1));
            idCreateDao.update(idCreate);
            ts.commitTransAction();
            return curValue;
        } catch (SQLException e) {
            logger.error("", e);
        }finally{
            try {
                ts.endTransAction();
            } catch (SQLException e) {
                logger.error("", e);
            }
        }
	    
		return null;
	}
	
	public IdCreate getIdCreate(String table){
	    Map map = new HashMap();
        map.put("name", table);
	    IdCreate idCreate = idCreateDao.get(map);
	    return idCreate;
	}
	
	public static void main(String[] args){
	    System.out.println("我是一只小小鸟！");
		IdCreateService testService = new IdCreateService();
		System.out.println(testService.getIdCreate("测试"));
		
		
	}

}
